import api from './index'
import YchConstant from "@/api/YchConstant";
import { axios } from '@/utils/request'

/**
 * login func
 * parameter: {
 *     username: '',
 *     password: '',
 *     remember_me: true,
 *     captcha: '12345'
 * }
 * @param parameter
 * @returns {*}
 */
export function login (parameter) {
  let url = '/login2';
  if (parameter) {
    let arr = [];
    if (parameter.sendid) {
      arr[arr.length] = "sendid=" + parameter.sendid;
      delete parameter.sendid;
    }
    if (parameter.verifycode) {
      arr[arr.length] = "verifycode=" + parameter.verifycode;
      delete parameter.verifycode;
    }
    if (parameter.username) {
      arr[arr.length] = "username=" + parameter.username;
    }
    if (arr.length > 0) {
      url = url + "?" + arr.join("&");
    }
  }
  return axios({
    url: YchConstant.PREFIX_SYSTEM + url,
    method: 'post',
    data: parameter
  })
}

export function codeLogin (parameter) {
  return axios({
    url: YchConstant.PREFIX_SYSTEM + '/loginByPasscode',
    method: 'post',
    data: parameter
  })
}

export function oauth2Login (parameter) {
  return axios({
    url: YchConstant.PREFIX_SYSTEM + '/oauth2/login',
    method: 'post',
    data: parameter
  })
}

export function dingtalkLogin (parameter) {
  return axios({
    url: YchConstant.PREFIX_SYSTEM + '/dingtalk/login',
    method: 'post',
    data: parameter
  })
}

export function phoneLogin (parameter) {
  return axios({
    url: YchConstant.PREFIX_SYSTEM + '/phoneLogin',
    method: 'post',
    data: parameter
  })
}

export function getSmsCaptcha (parameter) {
  return axios({
    url: api.SendSms,
    method: 'post',
    data: parameter
  })
}

export function getInfo () {
  return axios({
    url: '/api/user/info',
    method: 'get',
    headers: {
      'Content-Type': 'application/json;charset=UTF-8'
    }
  })
}

export function logout (logoutToken) {
  return axios({
    url: YchConstant.PREFIX_SYSTEM + '/logout',
    method: 'post',
    headers: {
      'Content-Type': 'application/json;charset=UTF-8',
      'X-Access-Token': logoutToken
    }
  })
}

export function thirdLogin (token) {
  return axios({
    url: `/thirdLogin/getLoginUser/${token}`,
    method: 'get',
    headers: {
      'Content-Type': 'application/json;charset=UTF-8'
    }
  })
}
